//
//  AreaASView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/19.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "AreaASView.h"
#import "ProvinceModel.h"
#import "CityModel.h"
#import "UIButton+BaseAttributes.h"
#import "UILabel+BasicAttributes.h"

@interface AreaASView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIPickerView *pickView;
@property (nonatomic,strong)NSArray *areaArr;
@property (nonatomic,strong)NSArray *areaChilderArr;
@property (nonatomic,strong)ProvinceModel *model;
@property (nonatomic,strong)ProvinceModel *tempModel;
@property (nonatomic,strong)ProvinceModel *returnModel;
@property (nonatomic,assign)NSInteger index1;
@property (nonatomic,assign)NSInteger index2;
@property (nonatomic,assign)NSInteger tempIndex;
@property (nonatomic,assign)BOOL firstLoad;

@property (nonatomic,copy)NSString *ownerText;
@property (nonatomic,copy)NSString *ownerId;
@end

@implementation AreaASView

- (NSArray *)areaArr
{
    if (!_areaArr) {
        _areaArr = [NSArray new];
    }
    return _areaArr;
}
- (NSArray *)areaChilderArr
{
    if (!_areaChilderArr) {
        _areaChilderArr = [NSArray new];
    }
    return _areaChilderArr;
}
- (UIPickerView *)pickView
{
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] init];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}
- (UIView *)dimView
{
    if(!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.0f;
        _dimView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_dimView];
    }
    return _dimView;
}
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baseView];
    }
    return _baseView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    _firstLoad = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
//        self.lineView.frame = CGRectMake(kScreenWidth*0.9, kScreenHeight - kScreenHeight*0.2, kScreenWidth*0.1, kScreenHeight);
        self.dimView.alpha = 0.20f;
    }];
    
    
    
    UIButton *cancelBtn = [UIButton bttonWithTitle:@"取消" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [cancelBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    
    
    UIButton *confirmBtn = [UIButton bttonWithTitle:@"完成" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [confirmBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    _titleLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:center];
    _titleLabel.text = self.title;
    [_baseView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(cancelBtn.mas_right);
        make.right.mas_equalTo(confirmBtn.mas_left);
        make.centerY.mas_equalTo(confirmBtn);
    }];
    
    [_baseView addSubview:self.pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_baseView);
        make.top.mas_equalTo(cancelBtn.mas_bottom);
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _titleLabel.text = _title;
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hidde];
}
- (void)show
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
        self.dimView.alpha = 0.20f;
    }];
}
- (void)hidde{
    if (_viewHadRemoveFromeSup) {
        _viewHadRemoveFromeSup();
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.dimView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (void)buttonDidClick:(UIButton *)sender
{
    [self hidde];

    if ([sender.titleLabel.text isEqualToString:@"取消"]) {
        [self.pickView reloadAllComponents];
        if (_resultModel) {
            _resultModel(_ownerText,_ownerId);
        }
    }else{
        _index1 = _returnModel.index1;
        _index2 = _returnModel.index2;
        if (_resultModel) {
            _resultModel(_returnModel.text,_returnModel.userId);
        }
    }
}

#pragma mark - UIPickView 代理 数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0) {
        if (self.areaArr.count) {
            return self.areaArr.count + 1;
        }else{
            return 0;
        }
        
    }else {
        return self.areaChilderArr.count + 1;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        if (row < self.areaArr.count && self.areaArr.count) {
            ProvinceModel *model = _areaArr[row];
            return model.text;
        }else if (row == self.areaArr.count&&self.areaArr.count){
           return @"全部";
        }else{
            return @"";
        }
    }else if(component == 1){
        if (self.areaChilderArr.count) {
            if (row == self.areaChilderArr.count) {
                return @"全部";
            }else{
                CityModel *chileModel = self.areaChilderArr[row];
                return chileModel.text;
            }
        }else{
            return @"全部";
        }
    }else{
        return @"";
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    ProvinceModel *btModel = [ProvinceModel new];
    
    if (component == 0) {
        _tempIndex = row;
        btModel.index1 = row;
        btModel.index2 = 0;
        if (self.areaArr.count == row&& self.areaArr.count) {
            self.areaChilderArr = [NSArray new];
            NSMutableString *allAreaString = [NSMutableString string];
            for (int i=0; i<self.areaArr.count; i++) {
                ProvinceModel *model = self.areaArr[i];
                if (i == self.areaArr.count-1) {
                    [allAreaString appendFormat:@"%@",model.userId];
                }else{
                    [allAreaString appendFormat:@"%@,",model.userId];
                }
            }
            btModel.text = @"全部";
            btModel.userId = allAreaString.copy;
        }else {
            self.tempModel = self.areaArr[row];
            btModel.text = self.tempModel.text;
            btModel.userId = self.tempModel.userId;
            self.areaChilderArr = self.tempModel.children;
            if (self.areaChilderArr.count == 0) {
                btModel.text = self.tempModel.text;
                btModel.userId = self.tempModel.userId;
            }else{
                CityModel *model2 = self.areaChilderArr[0];
                btModel.text = model2.text;
                btModel.userId = model2.userId;
            }
        }
        [self.pickView reloadComponent:1];
        [self.pickView selectRow:0 inComponent:1 animated:YES];
    }
    if (component == 1) {
        btModel.index1 = _tempIndex;
        btModel.index2 = row;
        if (self.areaChilderArr.count == row) {
             btModel = _tempModel;
        }else{
            CityModel *mode1 = self.areaChilderArr[row];
            btModel.text = mode1.text;
            btModel.userId = mode1.userId;
        }
    }
    
    _returnModel = btModel;
}
- (void)loadAreaBottomViewWithUsedArr:(NSArray *)arr
{
    self.areaArr = arr;
    if (_firstLoad) {
        _firstLoad = NO;
        _index1 = self.areaArr.count;
        _index2 = 0;
    }
    if (_index1 == self.areaArr.count) {
        NSMutableString *allAreaString = [NSMutableString string];
        for (int i=0; i<self.areaArr.count; i++) {
            ProvinceModel *model = self.areaArr[i];
            if (i == self.areaArr.count-1) {
                [allAreaString appendFormat:@"%@",model.userId];
            }else{
                [allAreaString appendFormat:@"%@,",model.userId];
            }
        }
        self.areaChilderArr = [NSArray array];
        _ownerText = @"全部";
        _ownerId = allAreaString.copy;
        if (_resultModel) {
            _resultModel(@"全部",allAreaString.copy);
        }
    }else{
        
        ProvinceModel *model1 = arr[_index1];
        self.tempModel = model1;
      
        self.areaChilderArr = model1.children;
        if (self.areaChilderArr.count == 0) {
            _index2 = 0;
            _ownerText = self.tempModel.text;
            _ownerId = self.tempModel.userId;
            if (_resultModel) {
                _resultModel(self.tempModel.text,self.tempModel.userId);
            }            
        }else{
            CityModel *model2 = self.areaChilderArr[_index2];
            _ownerText = model2.text;
            _ownerId = model2.userId;
            if (_resultModel) {
                _resultModel(model2.text,model2.userId);
            }
        }
    }
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:_index1 inComponent:0 animated:YES];
    [self.pickView selectRow:_index2 inComponent:1 animated:YES];
}
@end
